package com.openmanus.agent.service.impl;

import com.openmanus.agent.core.AgentState;
import com.openmanus.agent.entity.AgentEntity;
import com.openmanus.agent.mapper.AgentMapper;
import com.openmanus.agent.service.AgentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 智能体 Service 实现类
 * 
 * @author OpenManus Team
 * @version 1.0.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AgentServiceImpl implements AgentService {

    @Autowired
    private AgentMapper agentMapper;

    @Override
    public AgentEntity findByName(String name) {
        return agentMapper.findByName(name);
    }

    @Override
    public List<AgentEntity> findByType(String agentType) {
        return agentMapper.findByType(agentType);
    }

    @Override
    public List<AgentEntity> findByState(AgentState state) {
        return agentMapper.findByState(state);
    }

    @Override
    public boolean updateState(Long id, AgentState state) {
        int result = agentMapper.updateState(id, state);
        if (result > 0) {
            log.info("智能体状态更新成功: id={}, state={}", id, state);
            return true;
        } else {
            log.warn("智能体状态更新失败: id={}, state={}", id, state);
            return false;
        }
    }

    @Override
    public List<AgentEntity> findEnabledAgents() {
        return agentMapper.findEnabledAgents();
    }

    @Override
    public boolean registerAgent(AgentEntity agentEntity) {
        try {
            // 检查名称是否已存在
            AgentEntity existing = findByName(agentEntity.getName());
            if (existing != null) {
                log.warn("智能体名称已存在: {}", agentEntity.getName());
                return false;
            }

            // 设置默认值
            if (agentEntity.getState() == null) {
                agentEntity.setState(AgentState.IDLE);
            }
            if (agentEntity.getEnabled() == null) {
                agentEntity.setEnabled(true);
            }
            if (agentEntity.getMaxSteps() == null) {
                agentEntity.setMaxSteps(10);
            }
            if (agentEntity.getTimeoutSeconds() == null) {
                agentEntity.setTimeoutSeconds(300);
            }

            int result = agentMapper.insert(agentEntity);
            if (result > 0) {
                log.info("智能体注册成功: {}", agentEntity.getName());
                return true;
            } else {
                log.error("智能体注册失败: {}", agentEntity.getName());
                return false;
            }
        } catch (Exception e) {
            log.error("智能体注册异常: {}", agentEntity.getName(), e);
            return false;
        }
    }

    @Override
    public boolean enableAgent(Long id) {
        AgentEntity agent = agentMapper.findById(id);
        if (agent == null) {
            log.warn("智能体不存在: id={}", id);
            return false;
        }
        
        agent.setEnabled(true);
        int result = agentMapper.update(agent);
        if (result > 0) {
            log.info("智能体启用成功: id={}", id);
            return true;
        } else {
            log.warn("智能体启用失败: id={}", id);
            return false;
        }
    }

    @Override
    public boolean disableAgent(Long id) {
        AgentEntity agent = agentMapper.findById(id);
        if (agent == null) {
            log.warn("智能体不存在: id={}", id);
            return false;
        }
        
        agent.setEnabled(false);
        int result = agentMapper.update(agent);
        if (result > 0) {
            log.info("智能体禁用成功: id={}", id);
            return true;
        } else {
            log.warn("智能体禁用失败: id={}", id);
            return false;
        }
    }
}
